package tests;
import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 10715
 * Date: 2024-08-23
 * Time: 21:59
 */
public class EditPage extends Utils {
    public static String url = "http://192.168.47.135:8653/blog_system/blog_edit.html";
    Class<?> targetclass = EditPage.class;
    public EditPage() {
        super(url);
    }

    public  void  editByEditSuccess() throws InterruptedException {
        String methodName ="";
        try {
            methodName=targetclass.getMethod("editByEditSuccess").getName();
            getScreenShot(targetclass.getName()+"-"+methodName);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //更新数据
        driver.findElement(By.cssSelector("#title")).sendKeys("2024-test-editByEditSuccess");

        try {
            getScreenShot(targetclass.getName()+"-"+methodName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //点击提交
        WebDriverWait webDriverWait=new WebDriverWait(driver, Duration.ofSeconds(3));
        driver.findElement(By.cssSelector("#submit")).click();
        //有一个弹窗
        webDriverWait.until(ExpectedConditions.alertIsPresent());

        Alert alert =driver.switchTo().alert();
        alert.accept();

<<<<<<< HEAD
        Thread.sleep(1000);
        try {
            getScreenShot(targetclass.getName()+"-"+methodName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        driver.navigate().back();
    }
    public  void  editByNull(){
        driver.navigate().refresh();
        String methodName="";
        driver.findElement(By.cssSelector("#title")).sendKeys("");
        //弹出输入不合法，页面不变
        driver.findElement(By.cssSelector("#submit")).click();
        WebDriverWait webDriverWait =new WebDriverWait(driver,Duration.ofSeconds(3));
        webDriverWait.until(ExpectedConditions.alertIsPresent());
        Alert alert =driver.switchTo().alert();
        String val =alert.getText();
        alert.accept();
        //判断是否为原页面
        WebElement element = driver.findElement(By.cssSelector("#title"));
        assert  element.getText().equals("");
        //截屏
        try {
            methodName=targetclass.getMethod("editByNull").getName();
            getScreenShot(targetclass.getName()+"-"+methodName);
        } catch (IOException | NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        System.out.println("editByNull---"+val);
=======
        //无法输入博客内容---怎么办？
        //两个解决办法：
        //1）博客内容本身就有默认内容，我们不需要手动实现
        //2）通过键盘操作来实现 一般不用太复杂
//        WebElement ele = driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre"));
//        ele.sendKeys("rgdgffdhtfh");

       

        WebElement ele = driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre"));

        Actions actions = new Actions(driver);
        actions.keyDown(ele, Keys.CONTROL);

        driver.quit();
>>>>>>> 215c712139dc04c2aaaa1dac6d90346e6544d88c
    }
}